
package db;

import java.sql.*;
import java.util.*;

public class BaseDB {
    protected Connection con;
    
    //取得資料結果的欄位名稱
    public String[] dataTitles(ResultSet rs) throws SQLException{
        ArrayList<String> list=new ArrayList<String>();
        ResultSetMetaData rsmd = rs.getMetaData();
        for(int i = 1; i <= rsmd.getColumnCount(); i++)
            list.add(rsmd.getColumnName(i));
        return list.toArray(new String[list.size()]);        
    }
    //取得資料結果
    public ArrayList<HashMap<String,Object>> ResultSetToList(ResultSet rs) throws SQLException
    {   
        ArrayList<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
        String[] titles = dataTitles(rs);        
        while(rs.next()){
            HashMap<String,Object> map=new HashMap<String,Object>();
            for(String a : titles)
                map.put(a,rs.getObject(a));
            result.add(map);
        }        
        rs.close();
        return result;
    }
    
    public void dbClose() throws SQLException
    {
        this.con.close();
    }
}
